package ua.org.nuos.sdms.clientgui.client.handlers;

import ua.org.nuos.sdms.clientgui.client.context.Messages;
import ua.org.nuos.sdms.clientgui.client.context.UserContext;
import ua.org.nuos.sdms.clientgui.client.controller.GlobalBoard;
import ua.org.nuos.sdms.clientgui.client.events.ErrorMessageEvent;
import ua.org.nuos.sdms.clientgui.client.events.LogoutEvent;
import ua.org.nuos.sdms.clientgui.client.events.UserChangedEvent;
import ua.org.nuos.sdms.clientgui.client.listeners.LogoutListener;
import ua.org.nuos.sdms.clientgui.server.AppProxy;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: dio
 * Date: 19.02.12
 * Time: 20:36
 * To change this template use File | Settings | File Templates.
 */
public class LogoutHandler implements LogoutListener {
    private static final Logger logger = Logger.getLogger(LoginHandler.class.getName());

    @Override
    public void doLogout(LogoutEvent event) {
        UserContext userContext = AppProxy.getInstance().getUserContext();
        try {
            userContext.getServiceLocator().getLoginService().logout(userContext.getAppData().getRequest());
            userContext.getModelLocator().resetData();
            userContext.getComponentLocator().resetData();
            userContext.getControllerLocator().getController(GlobalBoard.ID).fire(new UserChangedEvent());
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Logout error", ex);
            AppProxy.getInstance().getControllerLocator().getController(GlobalBoard.ID).fire(new ErrorMessageEvent(Messages.ACTION_ERROR));
        }
    }
}
